<?php
class Merchant_Model_PlanUsage
{
    protected $_dbTable;

    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('非正确的table类型！');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Merchant_Model_DbTable_PlanUsage');
        }
        return $this->_dbTable;
    }

    //获取当前套餐使用情况
    public function getCurrentPlanUsage($customerId)
    {
        $select = $this->getDbTable()->select()->where("customer_id = ?",$customerId)
                                               ->where("active = 1");
        return  $this->_dbTable->fetchRow($select);
    }
    
    
    //按套餐id获取套餐使用情况
    public function getPlanUsageByPuId($puId)
    {
    	$select = $this->getDbTable()->select()->where("pu_id = ?",$puId);
    	return  $this->_dbTable->fetchAll($select);
    }
    
    
    //获取所有当天结算的套餐
    public function updatePlanUsage($customerId,$type,$qty)
    {
    	$where = $this->getDbTable()->getAdapter()->quoteInto("customer_id = ?",$customerId);
    	 $where .= $this->getDbTable()->getAdapter()->quoteInto(" and active = ?",1);
		 
		 $type = strtolower($type);
		 switch($type){
			case 'sku':
				$update = "update ".$this->getDbTable()->info("name")." set sku_used = sku_used + $qty where ".$where; 
				break;
			case 'item':
				$update = "update ".$this->getDbTable()->info("name")." set item_used = item_used + $qty where ".$where;
				break;
			case 'pallet':
				$update = "update ".$this->getDbTable()->info("name")." set pallet_used = pallet_used + $qty where ".$where;			
				break;	
			case 'outofsku':
				$update = "update ".$this->getDbTable()->info("name")." set sku_add_qty = sku_add_qty + $qty where ".$where;			
				break;	
			case 'outofitem':
				$update = "update ".$this->getDbTable()->info("name")." set item_add_qty = item_add_qty + $qty where ".$where;				
				break;
			case 'outofpallet':
				$update = "update ".$this->getDbTable()->info("name")." set pallet_add_qty = pallet_add_qty + $qty where ".$where;
				break;	
			default:
				$update = false;
				break;
				
		}
		if($update){
			return $this->getDbTable()->getAdapter()->query($update);	
		}
		return false;
     
    }
	
	 //获取所有当天结算的套餐
    public function update($puId,$row)
    {
    	$where = $this->_dbTable->getAdapter()->quoteInto('pu_id = ?',$puId);
 
		return $this->_dbTable->update($row, $where);
    }
    
    //更新套餐用量状态
    public function updatePlanUsageStatus($pu_id)
    {
    	$where = $this->_dbTable->getAdapter()->quoteInto("pu_id = ?",$pu_id);
    	return $this->_dbTable->update(array("active"=>"0","expired"=>date("Y-m-d H:i:s")),$where);
    }

	//Lock 并获取Plan
    public function lockCurrentPlan($customerId)
    {		 
    	$sql = "select * from plan_usage where customer_id='$customerId' and active='1' for update ";
        return  $this->_dbTable->getAdapter()->fetchRow($sql);
    }
	
	
	
	 public function getByCondition($condition,$type='*',$orderby="",$page=0,$pageSize=0){
        $select = $this->_dbTable->getAdapter()->select();
        $table  = $this->_dbTable->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
		if(isset($condition["customer_id"]) && $condition["customer_id"]!=''){
			$select->where(" customer_id =? ",($condition["customer_id"]));
		}
		if(isset($condition["active"]) && $condition["active"]!=''){
			$select->where(" active =? ",($condition["active"]));
		}
		
        if(isset($condition["expired-start"]) && $condition["expired-start"]!=''){
            $select->where(" TO_DAYS(expired) >= TO_DAYS(?) ",($condition["expired-start"]));
			if(isset($condition["expired-end"]) && $condition["expired-end"]!='')
				$select->where(" TO_DAYS(expired) <= TO_DAYS(?) ",($condition["expired-end"]));
			else
				$select->where(" TO_DAYS(expired) <= TO_DAYS(?) ",($condition["expired-start"]));
        } 
        if('count(*)'==$type){
            return $this->_dbTable->getAdapter()->fetchone($select);
        }else{
            if($orderby!=''){
                $orderbyArr = explode(',',$orderby);
                foreach($orderbyArr as $order){
                    $select->order($order);
                }
            }
            if($pageSize>0 && $page>0){
                $start = ($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
		    return $this->_dbTable->getAdapter()->fetchAll($select);
        }
	}
}